<?php
/*
 * Template Name: Archive
 */
/**
 * @abstract: product list follow category
 * @version: $Id$
*/
// Category list
add_style('css/archive.css');
get_header();

?>

	<div class="product-content">
		<div class="products-grids">
			<div class="container">
				<div class="alert alert-success" role="alert" style="background: #5BBD50; color: #FFF; font-weight: bold;"><?php
					if(is_category()){
						echo 'Tin rao trong mục: ',single_cat_title();
					} elseif (is_tag()){
						echo 'Tin rao trong mục: ',single_tag_title();
					} elseif (is_author()){
						the_post();
						echo 'Tin rao bởi: ',get_the_author();
						rewind_posts();
					} elseif (is_day()){
						echo 'Tin rao trong ngày: ',get_the_date('d/m/Y');
					} elseif (is_month()){
						echo 'Tin rao trong tháng: ',get_the_date('m/Y');
					} elseif (is_year()){
						echo 'Tin rao trong năm: ',get_the_date('Y');
					} else {
						echo 'Mục khác';
					}
					?>
				</div>

				<?php if(have_posts()):?>
				<div >Kế quả&nbsp;&nbsp;<strong><?php echo $wp_query->found_posts;?></strong>&nbsp;&nbsp;tin được tìm thấy</div>
				<?php while (have_posts()) :the_post();?>
				<div class="products-grid wow fadeInRight" data-wow-delay="0.4s">
					<div class="col-md-2 col-sm-2 product-img">
						<a href="<?php the_permalink();?>" title="<?php the_title();?>">
						<?php if(has_post_thumbnail()){
									the_post_thumbnail('medium');
								}else {
						?>
									<img alt="chotrois.com" src="<?php echo getFirstImageAttach($post->ID);?>">
						<?php } ?>
						</a>
					</div>
					<div class="col-md-7 col-sm-7 product-title">
						<div class="logo-title">
							<h2 style="font-size: 1.5em;"><a href="<?php the_permalink();?>" title="<?php the_title();?>"><?php the_title();?></a></h2>
						</div>
						<div class="rating">
							<?php the_time('g:i a - d/m/Y'); ?> bởi: <a style="color: #5BBD50;" href="<?php echo get_author_posts_url(get_the_author_meta('ID'));?>"><?php the_author();?></a> trong:
								<?php
								$categories = get_the_category();
								$saparator = ", ";
								$output = ' ';

								if($categories){
									foreach ($categories as $category){
										$output .= '<a style="color: #5BBD50;" href="'.get_category_link($category->term_id).'">'.$category->cat_name.'</a>'.$saparator;
									}
									echo trim($output, $saparator);
								}
								?>
						</div>
						<div class="content">
							<?php the_excerpt();?>
						</div>
					</div>
					<div class="col-md-3 col-sm-3 detail">
						<span style="font-size: 1em;">Giá: <label><?php echo get_post_meta($post->ID, CT_PRICE, true);?></label></span>
						<a href="<?php the_permalink();?>" title="<?php the_title();?>"><input type="button" value="Xem"></a>
					</div>
					<div class="clearfix"></div>
				</div>
				<?php endwhile;?>
				<?php else:?>
				<div class="products-grid wow fadeInRight" data-wow-delay="0.4s">
					<div class="alert alert-danger" role="alert">Chưa có tin rao</div>
				</div>
				<?php
				endif;

				// Pagination
				pagination();
				?>

			</div>
		</div>
	</div>
<?php get_footer('contact');?>
<?php get_footer();

/**
 * Get first image in 5 images which user can upload
 * @param unknown $postId
 * @return image path
 */
function getFirstImageAttach($postId) {

	$imgURI = ChotroiUtils::getAttachImage($postId);

	if (!is_array($imgURI))
		$imgURI = "";
	else if (!empty($imgURI))
		$imgURI = $imgURI[0];
	else {
		$imgURI = "";
	}

	return ChotroiUtils::getUploadImageURI($imgURI);
}

?>